
package logic.forms;

import javax.servlet.http.HttpServletRequest;

import logic.beans.User;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class UserForm extends ElementForm<User> {
	private static final long serialVersionUID = 1L;
	
	@Override
	public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		getElement().updatePassword("",request.getParameter("pass1"));
		
		if (getElement().getUserName() == null || getElement().getUserName().isEmpty()) {
			errors.add("userName",new ActionMessage("user.register.error.name.empty"));
		}
		
		if (getElement().checkPassword("")) {
			errors.add("password",new ActionMessage("user.register.error.password.empty"));
		} else {
			if (!getElement().checkPassword(request.getParameter("pass2"))) {
				errors.add("password",new ActionMessage("user.register.error.password.different"));
			}
		}
		
		return errors;
	}
	
	@Override
	public void setRequestFromElement(HttpServletRequest request) {
		request.setAttribute("userName",getElement().getUserName());
	}
}
